package edu.zju.gis.dbfg.server.controller;

import edu.zju.gis.dbfg.common.Page;
import edu.zju.gis.dbfg.common.Result;
import edu.zju.gis.dbfg.common.base.BaseController;
import edu.zju.gis.dbfg.common.constant.CodeConstants;
import edu.zju.gis.dbfg.server.model.MapService;
import edu.zju.gis.dbfg.server.model.search.MapServiceSearch;
import edu.zju.gis.dbfg.server.service.MapServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author Chenyr
 * @date 2019/9/11
 **/
@Slf4j
@CrossOrigin
@Controller
@RequestMapping("/mapservice")
public class MapServiceController extends BaseController<MapService, MapServiceService, String, MapServiceSearch> {
    @RequestMapping(value = "/public", method = RequestMethod.GET)
    @ResponseBody
    public Result getPublicModels(@SessionAttribute("userId") String userId, Page page) {
        Result<Page<MapService>> result = new Result<>();
        try {
            Page<MapService> maps = service.getServicesByRole(userId, page);
            result.setCode(CodeConstants.SUCCESS).setBody(maps);
        } catch (Exception e) {
            log.error("地图列表读取失败", e);
            result.setCode(CodeConstants.DAO_ERROR).setMessage("地图列表读取失败：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/personal", method = RequestMethod.GET)
    @ResponseBody
    public Result getPersonalModels(@SessionAttribute("userId") String userId, Page page) {
        Result<Page<MapService>> result = new Result<>();
        try {
            Page<MapService> maps = service.getServicesByUser(userId, page);
            result.setCode(CodeConstants.SUCCESS).setBody(maps);
        } catch (Exception e) {
            log.error("地图列表读取失败", e);
            result.setCode(CodeConstants.DAO_ERROR).setMessage("地图列表读取失败：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/{mapId}", method = RequestMethod.GET)
    @ResponseBody
    public Result getPersonalModels(@PathVariable("mapId") String mapId) {
        Result<MapService> result = new Result<>();
        try {
            MapService map = service.select(mapId);
            result.setCode(CodeConstants.SUCCESS).setBody(map);
        } catch (Exception e) {
            log.error("地图列表读取失败", e);
            result.setCode(CodeConstants.DAO_ERROR).setMessage("地图列表读取失败：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/public/search/{queryStr}", method = RequestMethod.GET)
    @ResponseBody
    public Result searchServicesByRole(@SessionAttribute("userId") String userId, @PathVariable("queryStr") String queryStr, Page page) {
        try {
            return Result.success().setBody(service.searchServicesByRole(userId, queryStr, page));
        } catch (Exception e) {
            log.error("地图列表读取失败", e);
            return Result.error().setMessage("地图列表读取失败：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/personal/search/{queryStr}", method = RequestMethod.GET)
    @ResponseBody
    public Result getPersonalModelsByStr(@SessionAttribute("userId") String userId, @PathVariable("queryStr") String queryStr, Page page) {
        try {
            return Result.success().setBody(service.searchServicesByUser(userId, queryStr, page));
        } catch (Exception e) {
            log.error("地图列表读取失败", e);
            return Result.error().setMessage("地图列表读取失败：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Result delete(@PathVariable("id") String id) {
        int result = service.delete(id);
        if (result == 1) {
            return Result.success().setMessage("删除成功!");
        } else {
            return Result.error().setMessage("删除失败!" );
        }

    }
}